package com.homestead.resp;

import com.homestead.constant.Constant;
import com.homestead.exception.ResponseErrorCode;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import java.io.Serializable;

/**
 * @author 1
 */
@Getter
@Setter
public class CommonResponse<T> implements Serializable {
    private static final long serialVersionUID = 7778346439490474738L;
    private String code;
    private String message;
    private T data;

    public CommonResponse() {
    }

    public CommonResponse(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public CommonResponse<T> success() {
        this.setCode(Constant.SUCCESS_CODE);
        this.setMessage(Constant.SUCCESS);
        this.setData(null);
        return this;
    }

    public CommonResponse<T> success(T obj) {
        return success("success", obj);
    }

    public CommonResponse<T> success(String message, T obj) {
        this.setCode(Constant.SUCCESS_CODE);
        this.setMessage(message);
        this.setData(obj);
        return this;
    }

    public CommonResponse<T> fail(ResponseErrorCode errorCode) {
        return fail(errorCode.getCode(), errorCode.getMessage());
    }

    public CommonResponse<T> fail(String code, String message) {
        this.setCode(code);
        this.setMessage(message);
        this.setData(null);
        return this;
    }
}
